// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <pollux/testing/gtest_utils.h>
#include <pollux/testing/functions/prestosql/function_base_test.h>

using namespace kumo::pollux::test;

namespace kumo::pollux::functions {
namespace {

class MultimapFromEntriesTest : public test::FunctionBaseTest {};

TEST_F(MultimapFromEntriesTest, basic) {
  auto row = [](std::vector<variant>&& inputs) {
    return variant::row(std::move(inputs));
  };

  auto input = make_array_of_row_vector(
      ROW({INTEGER(), VARCHAR()}),
      {
          // No duplicate keys.
          {row({1, "red"}), row({2, "blue"}), row({3, "green"})},
          // Some duplicate keys.
          {row({1, "pink"}),
           row({1, "red"}),
           row({2, "blue"}),
           row({2, variant::null(TypeKind::VARCHAR)}),
           row({2, "lightblue"}),
           row({3, "green"})},
          // Empty array.
          {},
      });

  auto result = evaluate("multimap_from_entries(c0)", make_row_vector({input}));

  auto expectedKeys = make_flat_vector<int32_t>({1, 2, 3, 1, 2, 3});
  auto expectedValues = make_nullable_array_vector<std::string>({
      {"red"},
      {"blue"},
      {"green"},
      {"pink", "red"},
      {"blue", std::nullopt, "lightblue"},
      {"green"},
  });

  auto expectedMap = make_map_vector({0, 3, 6}, expectedKeys, expectedValues);
  assertEqualVectors(expectedMap, result);
}

TEST_F(MultimapFromEntriesTest, nullKey) {
  auto row = [](std::vector<variant>&& inputs) {
    return variant::row(std::move(inputs));
  };

  auto input = make_array_of_row_vector(
      ROW({INTEGER(), VARCHAR()}),
      {
          {
              row({1, "red"}),
              row({variant::null(TypeKind::INTEGER), "blue"}),
              row({3, "green"}),
          },
      });

  POLLUX_ASSERT_THROW(
      evaluate("multimap_from_entries(c0)", make_row_vector({input})),
      "map key cannot be null");
}

TEST_F(MultimapFromEntriesTest, nullEntryFailure) {
  RowVectorPtr row_vector = make_row_vector(
      {make_flat_vector(std::vector<int64_t>{1, 2, 3}),
       make_flat_vector(std::vector<int64_t>{4, 5, 6})});
  row_vector->set_null(0, true);

  ArrayVectorPtr input = make_array_vector({0}, row_vector);

  POLLUX_ASSERT_THROW(
      evaluate("multimap_from_entries(c0)", make_row_vector({input})),
      "map entry cannot be null");

  // Test that having a null entry in the base vector used to create the array
  // vector does not cause a failure.
  input = make_array_vector({1}, row_vector);

  auto expectedKeys = make_flat_vector<int64_t>({2, 3});
  auto expectedValues = make_array_vector<int64_t>({{5}, {6}});

  auto expectedMap = make_map_vector({0}, expectedKeys, expectedValues);
  auto result = evaluate("multimap_from_entries(c0)", make_row_vector({input}));
  assertEqualVectors(expectedMap, result);
}

} // namespace
} // namespace kumo::pollux::functions
